package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.CPReportService;
import com.yixun.qfbao.apidto.AddBatchReportResDto;
import com.yixun.qfbao.apidto.BatchReportReqDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.enums.SystemIdEnum;
import com.yixun.qfbao.util.WxAccessTokenCacheUtils;
import com.yixun.qfbao.vo.BatchReportReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @classname: AddBatchReportHander.java
 * @author: Beau
 * @create: 2020/02/10 23:23
 * @version: 1.0.0
 **/
@Service("addBatchReportHander")
public class AddBatchReportHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(AddBatchReportHander.class);
    @Autowired
    private WxAccessTokenCacheUtils wxAccessTokenCacheUtils;
    @Autowired
    private CPReportService cpReportService;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        BatchReportReqVo reqDto1 = (BatchReportReqVo) reqDto;
        BatchReportReqDto params = reqDto1.getParams();
        BaseResponseDTO<AddBatchReportResDto> res = new BaseResponseDTO<AddBatchReportResDto>();
        AddBatchReportResDto resDto = new AddBatchReportResDto();
        try {
            //获取案场助手的微信accessToken
            String assistantAccessToken = wxAccessTokenCacheUtils.getAccessToken(params.getCompanyUuid(), SystemIdEnum.ASSISTANT.getCode(), null);
            resDto = cpReportService.addBatchReport(params, assistantAccessToken);
            res.setResult(ResultEnum.SUCCESS.getNo());
            if(resDto.getSaveReportCount() == 0){
                res.setResult(ResultEnum.FAIL.getNo());
            }
            res.setResData(resDto);
        } catch (Exception e) {
            logger.error("AddBatchReportHander error: {}", e);
            res.setResult(ResultEnum.FAIL.getNo());
            res.setErrorMsg("报备异常");
        }
        return res;
    }
}
